package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.activity.setting.agent.myguest.HierarchicalModeActivity;
import yt.kpool.adapter.ApiKeyListviewAdapter;
import yt.kpool.adapter.ModeSelectListviewAdapter;
import yt.kpool.bean.ApiKeyBean;
import yt.kpool.bean.RecommendCustomersBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * APIKey列表
 */
public class APIKeyListActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    @BindView(R.id.lv_select)
    ListView lv_select;
    private ApiKeyListviewAdapter apiKeyListviewAdapter;
    private List<ApiKeyBean.DataBean> list;

    public static void start(Activity mactivity, String ChildAccountID, String ChildAccountName) {
        Intent intent = new Intent(mactivity, APIKeyListActivity.class);
        intent.putExtra("ChildAccountID", ChildAccountID);
        intent.putExtra("ChildAccountName", ChildAccountName);
        mactivity.startActivity(intent);
    }



    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }
    public String getChildAccountName() {
        return getIntent().getStringExtra("ChildAccountName");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_only_read_list);
        ButterKnife.bind(this);
        initTitle();
        initlist();
        getdata();
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("只读页面链接");
        titleControl.setRightText("添加", new TitleControl.OnClickRight() {
            @Override
            public void onRight() {
               //添加

                //跳转生成Apikey界面
                CreateOnlyReadActivity.start(APIKeyListActivity.this,getChildAccountID(),getChildAccountName());
            }
        });
        loadingDialog = new LoadingDialog(this, 0);
    }
    private void initlist() {
        list=new ArrayList<>();
        apiKeyListviewAdapter = new ApiKeyListviewAdapter(this, list,getChildAccountID(),getChildAccountName());
        lv_select.setAdapter(apiKeyListviewAdapter);
    }

    /**
     * 获取APIKey列表数据
     */
    private void getdata() {

        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.ChildAccountAPIKeyList(getChildAccountID(),new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "获取APIKey列表数据：" + data);
                    ApiKeyBean apiKeyBean = MyGson.getInstance().fromJson(data, ApiKeyBean.class);
                    if (apiKeyBean.getType() == 1) {
                        if (apiKeyBean.getData().size() > 0) {

                            list.addAll(apiKeyBean.getData());
                            apiKeyListviewAdapter.notifyDataSetChanged();

                        } else {
                            ToastUtil.showToast(APIKeyListActivity.this, "没有可查询数据");
                        }
                    }else {

                        new ErrorDialog(APIKeyListActivity.this, apiKeyBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(APIKeyListActivity.this, msg);
                }
            });


        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 26) {

              list.clear();
              apiKeyListviewAdapter.notifyDataSetChanged();
              getdata();
        }
    }
}
